﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.Global;
using CE.Database;
using CE.CarInsurance;

public partial class carinsurance_ViewCIEnrollStatus : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            ClaerProcess();
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        ClaerProcess();
        if (txtEmail.Text.Trim() != "")
        {
            DataSet _dsEnrollStatus = new DataSet();
            EnrollStatusDB _enrollstatusdb = new EnrollStatusDB();
            Int64 _CECarId = 0;
            Int64 _ProviderCompanyId = 0;
            _dsEnrollStatus = _enrollstatusdb.GetEnrollStatus(txtEmail.Text.Trim());
            if (_dsEnrollStatus != null && _dsEnrollStatus.Tables.Count > 0)
            {
                if (_dsEnrollStatus.Tables[0].Rows.Count > 0)
                {
                    _CECarId = Convert.ToInt64(_dsEnrollStatus.Tables[0].Rows[0]["CECarId"].ToString());
                    _ProviderCompanyId = Convert.ToInt64(_dsEnrollStatus.Tables[0].Rows[0]["ProviderCompanyId"].ToString());
                    lblCIQuoteRefNo.Text = GetReferenceNumber(_CECarId, _ProviderCompanyId, "CI");
                    lblCIYes.Text = "Enrolled";
                    DateTime _SubmitedDate = Convert.ToDateTime(_dsEnrollStatus.Tables[0].Rows[0]["ModifiedDate"].ToString());
                    lblCISubmitedDate.Text = String.Format("{0:dd/MMM/yyyy hh:mm tt}", _SubmitedDate);
                    //lblCISubmitedDate.Text = _dsEnrollStatus.Tables[0].Rows[0]["SubmitedDate"].ToString();
                    lblCICompanyName.Text = GetProviderCompany(_ProviderCompanyId);
                }
                else
                {
                    lblCIQuoteRefNo.Text = "Not Applicable";
                    lblCINo.Text = "Not Enrolled";
                    lblCISubmitedDate.Text = "";
                    lblCICompanyName.Text = "";
                }
            }
            else
            {
                lblCIQuoteRefNo.Text = "Not Applicable";
                lblCINo.Text = "Not Enrolled";
                lblCISubmitedDate.Text = "";
                lblCICompanyName.Text = "";
            }

            if (_dsEnrollStatus != null && _dsEnrollStatus.Tables.Count > 1)
            {
                if (_dsEnrollStatus.Tables[1].Rows.Count > 0)
                {
                    _CECarId = Convert.ToInt64(_dsEnrollStatus.Tables[1].Rows[0]["PAId"].ToString());
                    _ProviderCompanyId = Convert.ToInt64(_dsEnrollStatus.Tables[1].Rows[0]["PACompanyId"].ToString());
                    lblPAQuoteRefNo.Text = GetReferenceNumber(_CECarId, _ProviderCompanyId, "PA");
                    lblPAYes.Text = "Enrolled";
                    DateTime _SubmitedDate = Convert.ToDateTime(_dsEnrollStatus.Tables[1].Rows[0]["ModifiedDate"].ToString());
                    lblPASubmitedDate.Text = String.Format("{0:dd/MMM/yyyy hh:mm tt}", _SubmitedDate);
                    //lblPASubmitedDate.Text = _dsEnrollStatus.Tables[1].Rows[0]["SubmitedDate"].ToString();
                    lblPACompanyName.Text = GetProviderCompany(_ProviderCompanyId);
                }
                else
                {
                    lblPAQuoteRefNo.Text = "Not Applicable";
                    lblPANo.Text = "Not Enrolled";
                    lblPASubmitedDate.Text = "";
                    lblPACompanyName.Text = "";
                }
            }
            else
            {
                lblPAQuoteRefNo.Text = "Not Applicable";
                lblPANo.Text = "Not Enrolled";
                lblPASubmitedDate.Text = "";
                lblPACompanyName.Text = "";
            }
        }
    }

    public string GetReferenceNumber(Int64 Number, Int64 ProviderId, string InsType)
    {
        string convertno = CE.Global.General.GetRefNumber(Number);
        string _RefNo = "";
        if(InsType == "CI")
        {
            if (ProviderId == 23)
            {
                _RefNo = "H" + convertno.ToString();
            }
            else if (ProviderId == 29)
            {
                _RefNo = "L" + convertno.ToString();
            }
            else if (ProviderId == 30)
            {
                _RefNo = "E" + convertno.ToString();
            }
        }
        else if (InsType == "PA")
        {
            if (ProviderId == 23)
            {
                _RefNo = "PAH" + convertno.ToString();
            }
            else if (ProviderId == 29)
            {
                _RefNo = "PAL" + convertno.ToString();
            }
            else if (ProviderId == 30)
            {
                _RefNo = "PAE" + convertno.ToString();
            }
        }
        return _RefNo;
    }

    public string GetProviderCompany(Int64 ProviderId)
    {
        string _Company = "";
        if (ProviderId == 23)
        {
            _Company = "HSBC Insurance";
        }
        else if (ProviderId == 29)
        {
            _Company = "Liberty Insurance";
        }
        else if (ProviderId == 30)
        {
            _Company = "Etiqa Insurance";
        }
        return _Company;
    }

    private void ClaerProcess()
    {
        lblCIYes.Text = "";
        lblCINo.Text = "";
        lblCIQuoteRefNo.Text = "";
        lblCISubmitedDate.Text = "";
        lblCICompanyName.Text = "";

        lblPAYes.Text = "";
        lblPANo.Text = "";
        lblPAQuoteRefNo.Text = "";
        lblPASubmitedDate.Text = "";
        lblCICompanyName.Text = "";
    }
}
